package com.dha.asynctask;

import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.dha.caodangcongnghe.LayDanhSachGiaoVien;
import com.dha.caodangcongnghe.R;
import com.dha.caodangcongnghe.XemThongBaoTheoNgayVaGiaoVien;
import com.dha.model.GiaoVienModel;
import com.dha.variable.DanhSachGiaoVienVariable;

public class DanhSachGiaoVienTheoNgayAsyncTask extends
		AsyncTask<Void, String, Integer> {

//	private ProgressDialog dialog;
	//
	// private XemThongBaoTheoNgayVaGiaoVien xemThongBaoTheoNgayVaGiaoVien;
	private String ngayDang;
	private String ngayDangDangChuan;

	private ArrayList<GiaoVienModel> arr;
	private ArrayAdapter<GiaoVienModel> adapter;

	private Activity activity;

	private final int KHONG_CO_GIAO_VIEN = 100;
	private final int OK = 101;
	private final int LOI_LAY_GIAO_VIEN = 102;

	// private ListView lvDanhSachGiaoVien;

	// public DanhSachGiaoVienAsyncTask(Activity activity) {
	// super();
	// this.activity = activity;
	// DanhSachGiaoVien.arr = new ArrayList<GiaoVienModel>();
	// adapter = new ArrayAdapter<GiaoVienModel>(activity,
	// android.R.layout.simple_list_item_1, DanhSachGiaoVien.arr);
	// lvDanhSachGiaoVien = (ListView) activity
	// .findViewById(R.id.lv_DanhSachGiaoVien);
	// lvDanhSachGiaoVien.setAdapter(adapter);
	// }

	public DanhSachGiaoVienTheoNgayAsyncTask(String ngay,
			ArrayList<GiaoVienModel> arr, ArrayAdapter<GiaoVienModel> adapter,
			Activity activity, String ngayDangDangChuan) {
		super();
		ngayDang = ngay;
		this.arr = arr;
		this.adapter = adapter;

		this.activity = activity;
		this.ngayDangDangChuan = ngayDangDangChuan;
	}

	@Override
	protected Integer doInBackground(Void... params) {
		SoapSerializationEnvelope envelope = null;
		try {
			SoapObject request = new SoapObject(
					DanhSachGiaoVienVariable.NAME_SPACE,
					DanhSachGiaoVienVariable.METHODNAME_THEONGAY);

			PropertyInfo ngayInfo = new PropertyInfo();
			ngayInfo.setName("ngay");
			ngayInfo.setValue(ngayDang);
			request.addProperty(ngayInfo);

			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			HttpTransportSE httpTransportSE = new HttpTransportSE(
					DanhSachGiaoVienVariable.URL);
			httpTransportSE.call(DanhSachGiaoVienVariable.SOAP_ACTION_THEONGAY,
					envelope);
			Vector<SoapObject> respone = (Vector<SoapObject>) envelope
					.getResponse();
			if (respone != null) {
				for (SoapObject so : respone) {
					publishProgress(so.getProperty("maGiaoVien").toString(), so
							.getProperty("tenGiaoVien").toString());
				}
				return OK;
			} else {
				return KHONG_CO_GIAO_VIEN;
			}
		} catch (ClassCastException cce) {
			try {
				SoapObject so = (SoapObject) envelope.getResponse();
				if (so != null) {
					publishProgress(so.getProperty("maGiaoVien").toString(), so
							.getProperty("tenGiaoVien").toString());
					return OK;
				} else {
					return KHONG_CO_GIAO_VIEN;
				}
			} catch (SoapFault e) {
				// TODO Auto-generated catch block
				return LOI_LAY_GIAO_VIEN;
			}

		} catch (Exception e) {
			return LOI_LAY_GIAO_VIEN;
		}
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		switch (result) {
		case KHONG_CO_GIAO_VIEN:
			Toast.makeText(
					activity,
					"Không có giáo viên gởi thông báo ngày "
							+ ngayDangDangChuan, Toast.LENGTH_LONG).show();

			break;
		case LOI_LAY_GIAO_VIEN:
			Toast.makeText(
					activity,
					"Lỗi trong việc lấy danh sách giáo viên", Toast.LENGTH_LONG).show();
			break;
		}

	}


	@Override
	protected void onProgressUpdate(String... values) {
		String maGiaoVien = values[0];
		String tenGiaoVien = values[1];

		GiaoVienModel gv = new GiaoVienModel(maGiaoVien, tenGiaoVien);

		arr.add(gv);

		adapter.notifyDataSetChanged();

		super.onProgressUpdate(values);
	}

}
